package com.xyebank.ledger.util;

import com.xyebank.ledger.enums.TaskStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class TaskRedisTool {
    
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 判断指定的job的T1是否在工作中
     *
     * @param jobType
     * @return
     */
    public boolean isT1Working(String jobType) {
        ValueOperations<String, TaskStatus> values = redisTemplate.opsForValue();
        TaskStatus o = values.get(buildKey(jobType, "T1_status"));
        if (o != null) {
            return TaskStatus.WORK.name().equals(o.name());
        }
        return false;
    }

    /**
     * 设置指定的job的T1状态为工作中
     *
     * @param jobType
     */
    public void setT1StatusFinWork(String jobType) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        values.set(buildKey(jobType, "T1_status"), TaskStatus.NONE.name());
    }


    /**
     * 构造key
     *
     * @param title
     * @param key
     * @return
     */
    private String buildKey(String title, String key) {
        return title + key;
    }

}
